package top.lyjwn.todo.template;

import com.github.pagehelper.Page;

import java.util.ArrayList;
import java.util.List;

public class RList<T> extends R {


    public RList() {
    }

    public RList(boolean done) {
        super(done);
    }

    public RList(boolean done, Integer code, String msg) {
        super(done, code, msg);
    }

    /**
     * 列表数据
     */
    protected List<T> list = new ArrayList();

    /**
     * 列表总数
     */
    protected long listTotal = 0;


    public long getListTotal() {
        return listTotal;
    }

    public RList<T> setListTotal(long listTotal) {
        this.listTotal = listTotal;
        return this;
    }

    public List<T> getList() {
        return list;
    }

    public RList<T> setList(List<T> list) {
        this.list = list;
        if (listTotal == 0 && this.list.size() > 0) {
            listTotal = this.list.size();
        }
        return this;
    }

    public RList<T> setList(Page<T> page) {
        if (page != null) {
            this.list = page.getResult();
            this.listTotal = page.getTotal();
        }
        return this;
    }

    public static RList init() {
        RList objectRList = new RList();
        return objectRList.ok("");
    }

    public static <M> RList<M> init(Class<M> clazz) {
        RList<M> objectRList = new RList<>();
        return objectRList.ok("");
    }

    public static <M> RList<M> init(Class<M> clazz, boolean done) {
        RList<M> objectRList = new RList<>(done);
        objectRList.error(500, "");
        return objectRList;
    }

    public static <M> RList<M> init(Class<M> clazz, boolean done, Integer code, String msg) {
        return new RList<>(done, code, msg);
    }

    @Override
    public RList<T> notLogin() {
        super.notLogin();
        return this;
    }

    @Override
    public RList<T> ok(String msg) {
        super.ok(msg);
        return this;
    }

    @Override
    public RList<T> error(Integer code, String msg) {
        super.error(code, msg);
        return this;
    }
}
